package dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.OrderDao;
import pojo.Order;
import pojo.OrderItem;

@Repository
public class OrderDaoImpl implements OrderDao {

	@PersistenceContext(name="un")
	private EntityManager manager;
	
	@Override
	public List<Order> findOrderByStatusByTime(String status, Date date) {
		String hq = "select o from Order o where status=? and createDate<?";
		List<Order> orderList = (List<Order>) manager.createQuery(hq).setParameter(1, status).setParameter(2, date).getResultList();
		return orderList;
	}
	
	@Override
	public List<Order> findOrderByStatus(String status,String mId) {
		String hq = "select o from Order o where status = ? and m_id = ?";
		List<Order> orderList = (List<Order>) manager.createQuery(hq).setParameter(1, status).setParameter(2, mId).getResultList();
		return orderList;
	}

	@Override
	public Order updateOrderStatus(String id,String status,String mId) {
		Order o = manager.find(Order.class,id);
	    o.setStatus(status); 
	    return o;
	}

	@Override
	public List<OrderItem> findOrderItem(String processNumber) {
		String hq = "select o.orderItemList from Order o where o.processNumber = ?";
		List<OrderItem> orderItemList = (List<OrderItem>) manager.createQuery(hq).setParameter(1, processNumber).getResultList();
		return orderItemList;
	}

	@Override
	public List<Order> findAll(String mId) {
		String hq = "select o from Order o where o.merchant.id=?";
		List<Order> orderList = (List<Order>) manager.createQuery(hq)
				.setParameter(1, mId)
				.getResultList();
		return orderList;
	}

	@Override
	public Order findByprocessNumber(String processNumber) {
		String hq = "select o from Order o where processNumber = ?";
		List<Order> orders =  manager.createQuery(hq).setParameter(1, processNumber).getResultList();
		if (orders.size()>0) {
			return orders.get(0);
		}
		return null;
	}

	

}
